import { ApiResponse, Profile, User } from '@/types/data'
import { RootThunkAction } from '@/types/store'
import request from '@/utils/request'
//用户自己信息的action
export const getUser = (): RootThunkAction => {
  return async dispatch => {
    const res = await request.get<ApiResponse<User>>('/user')
    dispatch({
      type: 'profile/getUser',
      payload: res.data.data,
    })
  }
}

//用户信息的个人资料
export const getUserProfile = (): RootThunkAction => {
  return async dispatch => {
    const res = await request.get<ApiResponse<Profile>>('/user/profile')
    dispatch({
      type: 'profile/getUserProfile',
      payload: res.data.data,
    })
  }
}
//修改昵称和简介-发送请求修改
export const updateUserProfile = (data: Partial<Profile>): RootThunkAction => {
  return async dispatch => {
    await request.patch('/user/profile', data)
    /* 重新獲取數據渲染 */
    dispatch(getUserProfile())
  }
}
//编辑用户照片资料（头像、身份证照片）
export const updataPhoto = (formData: FormData): RootThunkAction => {
  return async dispatch => {
    await request.patch('/user/photo', formData)
    //重新渲染
    dispatch(getUserProfile())
  }
}
